package com.cssnj.portal.web.home;

import com.cssnj.portal.domain.common.GeneralConstants;
import com.cssnj.portal.domain.common.UserInfo;
import com.cssnj.portal.service.home.HomeService;
import com.cssnj.portal.utils.RedisUtil;
import com.cssnj.portal.utils.TreeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.utility.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Controller
public class HomeController {

    @Autowired
    private Environment environment;

    @Autowired
    HomeService homeService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping({"/home.do"})
    public ModelAndView goHomePage(HttpServletRequest httpServletRequest)  throws JsonProcessingException {
        ModelAndView modelAndView = new ModelAndView("home/home");
        modelAndView.addObject("searchFlag","1");
        ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
        UserInfo userInfo =  (UserInfo)SecurityUtils.getSubject().getPrincipal();
        List list = userInfo.getLoginOutList();
        modelAndView.addObject("loginOutList",objectMapper.writeValueAsString(list));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping({"/gnTree.do"})
    public JsonResponse gnTree() throws SQLException, BaseException {
        JsonResponse jsonResponse = new JsonResponse();
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        List<Map<String, Object>> treelist = RedisUtil.getUserTreelist(redisTemplate,userInfo.getSwrySfDm());
        jsonResponse.getRepData().put("treelist", TreeUtil.toChlidrenListTree(treelist,"closed"));
        return jsonResponse;
    }

    @RequestMapping({"/main.do"})
    public ModelAndView main(HttpServletRequest httpServletRequest) throws SQLException, BaseException, JsonProcessingException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.setHandleCode("zxInfo");
        JsonResponse jsonResponse = homeService.execute(jsonRequest);
        ModelAndView modelAndView;
        ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
        Map<String, Object> repData = jsonResponse.getRepData();
        if ("3".equals(repData.get("zxlx_dm"))) {
            modelAndView = new ModelAndView("home/mainGnCharts");
        } else if ("2".equals(repData.get("zxlx_dm"))) {
            modelAndView = new ModelAndView("home/mainCharts");
        } else {
            modelAndView = new ModelAndView("home/mainGn");
        }
        modelAndView.getModelMap().put("repData", objectMapper.writeValueAsString(repData));

        jsonRequest.setHandleCode("getTjGn");
        jsonResponse = homeService.execute(jsonRequest);
        List<Map<String, Object>> list = (List<Map<String, Object>>)jsonResponse.getRepData().get("tjgnList");
        modelAndView.getModelMap().put("tjgnList", objectMapper.writeValueAsString(list));

        return modelAndView;
    }


    @RequestMapping({"/smOut.do"})
    public ModelAndView gosmPage() {
        ModelAndView modelAndView = new ModelAndView("home/smOut");
        return modelAndView;
    }

    @RequestMapping({"/syOut.do"})
    public ModelAndView gosyPage() {
        ModelAndView modelAndView = new ModelAndView("home/syOut");
        return modelAndView;
    }
}
